package view.UIlayer
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import vo.GameMode;
	
	public class ScoreboardView extends Sprite
	{
		private var gameMode:GameMode = GameMode.getInstance();
		private var winOrLose:MovieClip;
		
		private var chipLabel:TextField = new TextField();
		private var winChipLabel:TextField = new TextField();
		private var allChipsLabel:TextField = new TextField();		
		private var winChipText:TextField = new TextField();
		private var chipText:TextField = new TextField();
		private var allChipsText:TextField = new TextField();
		
		public static const SCOREBOARD_Y:Number = 553;
		public static const SCOREBOARDBO_Y:Number = 568;
		
		public function ScoreboardView()
		{
			this.addEventListener(Event.ADDED_TO_STAGE,addHandler);
		}
		
		public function updateGameStart():void{
			winOrLose.visible = false;			
			updateScore();
		}
		
		public function updateGameEnd():void{
			winOrLose.visible = true;			
			updateScore();
			playMusic();
		}
		
		public function updateScore():void{
			chipText.text =  gameMode.chip.toString();	
			allChipsText.text = gameMode.allChips.toString();
			winChipText.text = gameMode.winChip.toString();	
		}
		
		public function playMusic():void{	
			if(gameMode.isWin){					
				winOrLose.gotoAndStop("赢");
				gameMode.playMe("assets/sound/sound_237.mp3");
				
			}
			else{
				winOrLose.gotoAndStop("输");
				gameMode.playMe("assets/sound/sound_235.mp3");
			}		
			gameMode.playMe("assets/sound/sound_2_(flip).mp3");
		}
		
		private function init():void{
			
			winOrLose = new whoWinBtn();		
			winOrLose.x = stage.stageWidth/2;
			winOrLose.y = 280;
			addChild(winOrLose);
			winOrLose.visible = false;
			
			chipLabel.htmlText =  <span> <font color = "#fee303">  筹码: </font ></span>;
			winChipLabel.htmlText = <span> <font color = "#fee303"> 获筹:</font ></span>;
			allChipsLabel.htmlText = <span> <font color = "#fee303"> 总筹码:</font ></span>;
			winChipText.text = gameMode.winChip.toString();	
			winChipText.border = true;
			winChipText.borderColor = 0x606060;
			winChipText.width = 28;
			winChipText.height = 18;
			
			var formatter:TextFormat = new TextFormat( );
			
			formatter.color = 0xFFFFFF; 
			formatter.blockIndent = 5; 
			winChipText.setTextFormat(formatter);
			
			chipText.text =  gameMode.chip.toString();	
			chipText.setTextFormat(formatter);
			
			chipText.border = true;
			chipText.borderColor = 0x606060;
			chipText.width = 28;
			chipText.height = 18;
			
			allChipsText.text = gameMode.allChips.toString();
			allChipsText.setTextFormat(formatter);
			allChipsText.border = true;
			allChipsText.borderColor = 0x606060;
			allChipsText.width = 38;
			allChipsText.height = 18;
			
			winChipLabel.y = SCOREBOARD_Y;
			winChipText.y = SCOREBOARDBO_Y;
			
			chipLabel.y = SCOREBOARD_Y;
			chipText.y = SCOREBOARDBO_Y;
			
			allChipsLabel.y = SCOREBOARD_Y;
			allChipsText.y = SCOREBOARDBO_Y;
			
			winChipLabel.x = 215;
			winChipText.x = 255;
			
			chipLabel.x = 285;
			chipText.x = 325;
			
			allChipsLabel.x = 355;
			allChipsText.x = 405;
			
			addChild(winChipLabel);
			addChild(chipLabel);
			addChild(allChipsLabel);
			
			addChild(winChipText);
			addChild(chipText);
			addChild(allChipsText);
		}
		
		private function addHandler(e:Event):void{
			init();
		}
	}
}